<%--
  Created by IntelliJ IDEA.
  User: M.c
  Date: 2016/9/7
  Time: 15:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <style type="text/css">
        body, html, #allmap {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
            font-family: "微软雅黑";
        }
    </style>
    <script type="text/javascript"
            src="http://api.map.baidu.com/api?v=2.0&ak=tAQAy4bH7My5lQL3NttMcQZGqwwGxvOl"></script>
    <title>添加动画标注点</title>
</head>
<body>
<button onclick="send()">搜索老公</button>
<div id="message"></div>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    map.enableScrollWheelZoom(true);//设置可拖动放大缩小


    //添加点
    function addMarker(point) {
        map.centerAndZoom(point, 15);
        var marker = new BMap.Marker(point);
        marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
        map.addOverlay(marker);

    }

    function deletePoint(){
        var allOverlay = map.getOverlays();
        for (var i = 0; i < allOverlay.length -1; i++){
            map.removeOverlay(allOverlay[i]);
        }
    }



//    websocket方式-=================================================================================
    var jsonMsg = null;
    var websocket = null;
    //判断当前浏览器是否支持WebSocket
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://139.196.216.185:80/location/websocket");
    }
    else {
        alert('当前浏览器 Not support websocket')
    }

    //连接发生错误的回调方法
    websocket.onerror = function () {
        setMessageInnerHTML("WebSocket连接发生错误");
    };

    //连接成功建立的回调方法
    websocket.onopen = function () {
        setMessageInnerHTML("WebSocket连接成功");
    }

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
//        setMessageInnerHTML(event.data);
        if("1"!=event.data){
            deletePoint()
            jsonMsg = JSON.parse(event.data);
            setMessageInnerHTML("当前坐标: "+jsonMsg.lng+","+jsonMsg.lat);
            var point = new BMap.Point(jsonMsg.lng, jsonMsg.lat);
            addMarker(point);
        }

    }

    //连接关闭的回调方法
    websocket.onclose = function () {
        setMessageInnerHTML("WebSocket连接关闭");
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        closeWebSocket();
    }

    //将消息显示在网页上
    function setMessageInnerHTML(innerHTML) {
        document.getElementById('message').innerHTML += innerHTML + '<br/>';
    }

    //关闭WebSocket连接
    function closeWebSocket() {
        websocket.close();
    }

    //发送消息
    function send() {
        var message = "1";
        websocket.send(message);
    }
</script>


